package com.ihr360.payroll.model.dto.salary.meta;

import com.ihr360.authority.sharing.model.IResourceControl;
import com.ihr360.authority.sharing.model.ResourceAccess;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.entity.NumberIdWithTime;
import com.ihr360.commons.model.type.SalaryAdjustFormula;
import com.ihr360.payroll.model.base.salaryreport.IPayrollConfig;
import com.ihr360.payroll.model.vo.PayrollConfigVo;
import com.ihr360.payroll.model.vo.SalaryFieldVo;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.ArrayList;
import java.util.List;


/**
 * 工资表配置，HR用
 */
@Getter
@Setter
@MappedSuperclass
public class AbstractPayrollConfig extends NumberIdWithTime<Long> implements IPayrollConfig, IResourceControl {

    /**
     * 名称
     */
    private String name;

    /**
     * 是否启用
     */
    private boolean enabled = true;

    @Enumerated(EnumType.STRING)
    private SalaryAdjustFormula salaryAdjustFormula;

    /**
     * 调整基数引用字段
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<SalaryField> salaryAdjustReferenceFields;

    /**
     * 字段
     */
    @OrderBy("indexno")
    @OneToMany(mappedBy = "config", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<SalaryField> fields = new ArrayList<>();

    @Embedded
    private ResourceAccess resourceAccess;


    @Override
    public ResourceAccess getResourceAccess() {
        if (resourceAccess == null) {
            resourceAccess = new ResourceAccess();
        }
        return resourceAccess;
    }

    @Override
    public void setResourceAccess(ResourceAccess resourceAccess) {
        this.resourceAccess = resourceAccess;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public void setSalaryAdjustFormula(SalaryAdjustFormula salaryAdjustFormula) {
        this.salaryAdjustFormula = salaryAdjustFormula;
    }

    public void setFields(List<SalaryField> fields) {
        this.fields = fields;
    }

    public List<SalaryFieldVo> getSalaryAdjustReferenceFieldVos(){
        List<SalaryFieldVo> salaryFieldVoList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(salaryAdjustReferenceFields)) {
            salaryAdjustReferenceFields.forEach(field -> {
                PayrollConfigVo payrollConfig = null;
                if (field.getConfig() != null) {
                    PayrollConfig config = field.getConfig();
                    payrollConfig = new PayrollConfigVo(config.getId(), config.getName(), new ArrayList<>(),null);
                }
                salaryFieldVoList.add(new SalaryFieldVo(field.getId(), field.getCode(), payrollConfig, field.getMeta(), field.getSource(),
                        field.getValueMeta(), field.getName(), field.getType(), field.getTaxType(), field.isTaxMergeField(), field.getPersonalSalaryNumOp(),
                        field.getCompanyCostNumOp()));
            });
        }
        return salaryFieldVoList;
    }
}
